package com.woniuxy.reader.service.impl;

import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.woniuxy.reader.entity.Author;
import com.woniuxy.reader.entity.Book;
import com.woniuxy.reader.entity.User;
import com.woniuxy.reader.mapper.AuthorMapper;
import com.woniuxy.reader.mapper.BookMapper;
import com.woniuxy.reader.mapper.UserMapper;
import com.woniuxy.reader.service.AuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.reader.vo.BookVO;
import com.woniuxy.reader.vo.BookVOO;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zh_o
 * @since 2020-10-09
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author> implements AuthorService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AuthorMapper authorMapper;
    @Resource
    private BookMapper bookMapper;

    @Override
    public List<Author> selectAuthor(String name) {
        QueryWrapper<Author> wrapper = new QueryWrapper<Author>();
        wrapper.like(StringUtils.isNotBlank(name), "pen_name", name);
        return authorMapper.selectList(wrapper);
    }

    @Override
    public List<BookVOO> selectSigning() {
        return authorMapper.selectByEditorTel();
    }

    @Override
    public String login(String tel, String password) {
        Author author = authorMapper.selectOne(new QueryWrapper<Author>().eq("author_tel", tel));
        if (author == null) {
            return "该用户不存在";
        } else {
            Md5Hash md5Hash = new Md5Hash(password, "woNiuReAdeR*79", 1024);
            if (author.getPassword().equals(md5Hash.toString())) {
                return "作者登录成功";
            } else {
                return "密码错误";
            }
        }
    }

    /**
     　* @Author daxigua
     * @Description  通过传入作者id，查询作品进行判断，是否满足签约条件，true满足，flase不满足
     * @Date 2020/10/15  20:11
     * @Param  * @param null
     * @return
     */
    @Override
    public boolean selectBookByAuthorIdAndCountAndStatus(int id) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("author_id",id);
        wrapper.eq("status",1);
        wrapper.ge("count",30000);
        List<Book> books = bookMapper.selectList(wrapper);

        if (books.size()>=1){
            return true;
        }

        return false;
    }


    /**
     　* @Author daxigua
     * @Description 传入作者名，进行模糊查询，查找所有包含改字符串的作者
     * @Date 2020/10/16  14:55
     * @return
     */
    @Override
    public List<Author> selectAuthorLikeName(String name) {
        QueryWrapper<Author> wrapper = new QueryWrapper<>();
        wrapper.like("pen_name",name);

        return authorMapper.selectList(wrapper);
    }

    @Override
    public Boolean SelectByPenName(String penName) {
        QueryWrapper<Author> wrapper = new QueryWrapper<>();
        wrapper.eq("pen_name",penName);
        return authorMapper.selectList(wrapper).isEmpty();
    }

    @Override
    public String insertAuthor(Author author,Integer userId) {
        User user=userMapper.selectById(userId);
        author.setPassword(user.getPassword());
        author.setAuthorTel(user.getUserTel());
        if(IdcardUtil.getAgeByIdCard(author.getIdCard())>18){
            authorMapper.insert(author);
            return "注册成功！";
        }else{
            return "不满足注册条件";
        }
    }

    @Override
    public String SginAuthor(int authorid,int editorid) {
        authorMapper.SginAuthor(authorid,editorid);
        return "签约成功";
    }
}
